<?php

class CustomerAccountingController extends Controller {

    public function actionlistqb($custno, $query = NULL, $page = 1, $sort = NULL, $dir = NULL) {
        $customer = Customers::model()->findByPk($custno);
        $criteria = new CDbCriteria();
        $criteria->condition = "customerRefFullName ='$customer->lastname $customer->custno'";
        $criteria->order = "date, paymentTxnID ASC";
        $criteria->group = 'paymentTxnID';
        if ($page > 0)
            $page--;
        $data = new CActiveDataProvider('QbPaymentsapplied', array(
                    'criteria' => $criteria,
                    'pagination' => array(
                        'pageSize' => 20,
                        'currentPage' => $page,
                    )
                ));
        $rs = array();
        if (count($data->getData()) > 0) {
            $rs['totalCount'] = $data->getTotalItemCount();
            $rs['type'] = 'QB payments';
            foreach ($data->getData() as $i => $item) {
                $rs['data'][$i] = $item->getAttributes();
                $rs['data'][$i]['invoiceAmount'] = 0;
                $models = QbPaymentsapplied::model()->findAll("refNumber ='$item->refNumber'");
                foreach ($models as $model) {
                    $rs['data'][$i]['invoiceAmount']+= $model->invoiceAmount;
                }
            }
            echo json_encode($rs);
        }
        Yii::app()->end();
    }

    public function actionPayments($ref, $query = NULL, $page = 1, $sort = NULL, $dir = NULL) {
        $criteria = new CDbCriteria();

        $criteria->condition = "refNumber ='$ref'";
        $criteria->order = "date, paymentTxnID ASC";

        if ($page > 0)
            $page--;
        $data = new CActiveDataProvider('QbPaymentsapplied', array(
                    'criteria' => $criteria,
                    'pagination' => array(
                        'pageSize' => 20,
                        'currentPage' => $page,
                    )
                ));
        $rs = array();
        if (count($data->getData()) > 0) {
            $rs['totalCount'] = $data->getTotalItemCount();
            $rs['type'] = 'QB payments';
            foreach ($data->getData() as $i=>$item) {
                $rs['data'][$i] = $item->getAttributes();
                $invoice = Invoice::model()->findByPk($item->invoiceNo);
                if (is_null($invoice)) $rs['data'][$i]['orgAmount'] =0;
                else
                $rs['data'][$i]['orgAmount'] = $invoice->amountcharged;
            }
            echo json_encode($rs);
        }
        Yii::app()->end();
    }

}